package sc
{
	import com.adobe.share.api.ShareAPI;
	import com.adobe.share.api.ShareAPIUser;
	
	import flash.events.*;
	import flash.filesystem.*;
	import flash.net.FileFilter;
	
	import mx.controls.Alert;
	import mx.events.*;
	public class Files
	{
		private var currentFile:File; // The currentFile opened (and saved) by the application
		private var stream:FileStream = new FileStream(); // The FileStream object used for reading and writing the currentFile
		public var defaultDirectory:File; // The default directory
		[Bindable] public var dataChanged:Boolean = false; // Whether the text data has changed (and should be saved)
		private var api : ShareAPI = null;
		private var user : ShareAPIUser = null;
		private var texyFilt:FileFilter = new FileFilter("Texy", "*.texy;*.flexy;*.txt");
		
		public var template:String = "\n---|#metadata|---\nWarning, end of user-readable data, please ignore the following text.\n\n";
		public var tempReg:RegExp = /---\|#metadata\|---\nWarning, end of user-readable data, please ignore the following text.\n\n/;
		
		public function Files()
		{
			defaultDirectory = File.documentsDirectory;
		}
		
		/**
		* Called when the user clicks the Open button. Opens a file chooser dialog box, in which the 
		* user selects a currentFile. 
		*/
		public function openFile(event:* = null):void 
		{	
			var fileChooser:File;
			if (currentFile) 
			{
				fileChooser = currentFile;
			}
			else
			{
				fileChooser = defaultDirectory;
			}
			fileChooser.browseForOpen("Open", [texyFilt]);
			fileChooser.addEventListener(Event.SELECT, fileOpenSelected);
		}
		/**
		* Called when the user selects the currentFile in the FileOpenPanel control. The method passes 
		* File object pointing to the selected currentFile, and opens a FileStream object in read mode (with a FileMode
		* setting of READ), and modify's the title of the application window based on the filename.
		*/
		public function fileOpenSelected(event:Event):void 
		{
			open((event.target as File));
			
			//title = "Text Editor - " + currentFile.name;
			
		}
		public function open(file:File):void
		{
			currentFile = file;
			stream = new FileStream();
			stream.openAsync(currentFile, FileMode.READ);
			stream.addEventListener(Event.COMPLETE, fileReadHandler);
			stream.addEventListener(IOErrorEvent.IO_ERROR, readIOErrorHandler);
			dataChanged = false;
			currentFile.removeEventListener(Event.SELECT, fileOpenSelected);
			var nam:String = currentFile.name;
			newDoc();
			var hell:texy_editor_tab = $.main.tabCont.selectedChild as  texy_editor_tab;
			
			hell.label = nam;
		}
		/**
		* Called when the stream object has finished reading the data from the currentFile (in the openFile()
		* method). This method reads the data as UTF data, converts the system-specific line ending characters
		* in the data to the "\n" character, and displays the data in the mainTextField Text component.
		*/
		public function fileReadHandler(event:Event):void 
		{
			var str:String = stream.readUTFBytes(stream.bytesAvailable);
			stream.close();
			var lineEndPattern:RegExp = new RegExp(File.lineEnding, "g");
			str = str.replace(lineEndPattern, "\n");
			
			
			
			var hell:texy_editor_tab = $.main.tabCont.selectedChild as  texy_editor_tab;
			if(str.search(tempReg) != -1) {
				var st:Array = str.split(tempReg);
				trace(st.length);
				str = st[0];
				hell.template.text = st[1];
				hell.tempChanged = true;
			}
			hell.inp.text = str; 
			stream.close();
			App.sendit(new MouseEvent(MouseEvent.CLICK));
		}
		/**
		* Called when the user clicks the "Save" button. The method sets up the stream object to point to
		* the currentFile specified by the currentFile object, with save access. This method converts the "\r" and "\n" characters 
		* in the mainTextField.text data to the system-specific line ending character, and writes the data to the currentFile.
		*/
		public function saveFile(event:* = null):void 
		{
			var hell:texy_editor_tab = $.main.tabCont.selectedChild as  texy_editor_tab;
			hell.defaultDirectory = defaultDirectory;
			hell.saveFile();
		}
		/**
		* Called when the user clicks the "Save As" button. Opens a Save As dialog box, in which the 
		* user selects a currentFile path. See the FileSavePanel.mxml currentFile.
		*/
		public function saveAs():void 
		{
			var hell:texy_editor_tab = $.main.tabCont.selectedChild as  texy_editor_tab;
			hell.defaultDirectory = defaultDirectory;
			hell.saveFile();
		}
		
		/**
		* Handles I/O errors that may come about when opening the currentFile.
		*/
		public function readIOErrorHandler(event:Event):void 
		{
			
			Alert.show("The specified currentFile cannot be opened.", "Error", Alert.OK, $.main);
		}
		
		public function newDoc(event:* = null):void {
			var nd:texy_editor_tab = new texy_editor_tab();
			$.main.tabCont.addChild(nd);
			if($.main.tabCont.numChildren > $.main.tabCont.selectedIndex + 1) 
			{
				$.main.tabCont.selectedIndex++;
			}
			init();
			currentFile = undefined;
		}
		
		public function init():void {
			var hell:texy_editor_tab = $.main.tabCont.selectedChild as  texy_editor_tab;
			hell.sub.addEventListener(MouseEvent.CLICK, App.sendit);
		
		}	
		
			
	}
}